package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author danzhiheng
 * @version 1.1
 * @date 2023/2/4 19:35
 */
@RestController
public class ApiController {

    @Resource
    private WmUserService wmUserService;

    @Resource
    private WmMaterialService wmMaterialService;

    @Resource
    private ApiService apiService;

    /**
     * 通过apUserId查询自媒体记录
     * @param apUserId
     * @return
     */
    @GetMapping("api/wmUser/{apUserId}")
    public ResultVo<WmUser> getByApUserId(@PathVariable(value = "apUserId") Long apUserId){
        //1. 调用业务层
        WmUser wmUser = wmUserService.getById(apUserId);
        //2. 响应结果
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加自媒体人
     * @param wmUser
     * @return
     */
    @PostMapping("api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial){
        if(wmMaterialService.save(wmMaterial)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 查询所有的频道
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> channelList(){
        List<AdChannel> channelList = apiService.getChannelList();
        return ResultVo.ok(channelList);
    }
}
